﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using THenu.TinyHourseLib.DesignPattern.Behavioral_Patterns.Interpreter_Pattern.RealWorldCode;

namespace THenu.TinyHourseLib.DesignPattern.Behavioral_Patterns.Interpreter_Pattern
{
    class InterpreterPattern : IPatternDemonstrate
    {
        public void StructuralCode()
        {
            Context context = new Context();

            // Usually a tree
            ArrayList list = new ArrayList();

            // Populate 'abstract syntax tree' 
            list.Add(new TerminalExpression());
            list.Add(new NonterminalExpression());
            list.Add(new TerminalExpression());
            list.Add(new TerminalExpression());

            // Interpret
            foreach (AbstractExpression exp in list)
            {
                exp.Interpret(context);
            }
        }

        public string PatternDescription()
        {
            string description = @"
Definition
Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.";
            return description;
        }

        public void RealWorldExample()
        {
            string roman = "MCMXXVIII";
            RealWorldCode.Context context = new RealWorldCode.Context(roman);

            // Build the 'parse tree'
            List<Expression> tree = new List<Expression>();
            tree.Add(new ThousandExpression());
            tree.Add(new HundredExpression());
            tree.Add(new TenExpression());
            tree.Add(new OneExpression());

            // Interpret
            foreach (Expression exp in tree)
            {
                exp.Interpret(context);
            }

            Console.WriteLine("{0} = {1}",
              roman, context.Output);
        }
    }
}
